package com.good.shopping.order.web;

import com.good.common.to.MemberPrice;
import com.good.shopping.order.entity.OrderEntity;
import com.good.shopping.order.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * 测试消息队列 、 分布式事务 Seate
 * @author: yan
 **/

@Controller
public class HelloController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @GetMapping(value = "/test/createOrder")
    public String createOrderTest() {

        //订单下单成功
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());

        //给MQ发送消息
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);

        return "ok";
    }

    // 返回页面
    @GetMapping(value = "/{page}.html")
    public String listPage(@PathVariable("page") String page) {
        return page;
    }


    // 测试分布式事务 Seata
    @ResponseBody
    @GetMapping(value = "/testSeata")
    public String testSeata() {
        orderService.doTestSeata();
        return "ok";
    }

    @ResponseBody
    @GetMapping("/testMq")
    public String testMq(){
        //订单下单成功
        MemberPrice obj = new MemberPrice();
        obj.setName(UUID.randomUUID().toString());
        obj.setPrice(new BigDecimal("3"));
        //给MQ发送消息 参数 ：交换机  路由件  对象
        rabbitTemplate.convertAndSend("stock-event-exchange","stock.locked",obj);
        return "ok";
    }




}
